package com.ist412.pocke;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author Rob C
 * @version 1.0
 */
public class ExerciseActivity extends Activity implements OnClickListener {
	
	protected enum 			Screens { MUSCLEGROUP, EXERCISE, ADD };
	protected LinearLayout 	_llyMain;
	private Screens			_currentScreen;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Remove title & Make fullscreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
	    setContentView(R.layout.activity_exercise);
	    
	    _llyMain = (LinearLayout) findViewById(R.id.exercise_llyMain);
	    _currentScreen = Screens.MUSCLEGROUP;
	    
	    ImageView imgBack		= (ImageView) findViewById(R.id.exercise_imgBack);
	    ImageView imgSettings	= (ImageView) findViewById(R.id.exercise_imgSettings);
	    
	    imgBack.setOnClickListener(this);
	    imgSettings.setOnClickListener(this);
	    
	    // Setup navigation
        ImageView imgProfile 	= (ImageView) findViewById(R.id.navigation_imgProfile);
        ImageView imgCalendar 	= (ImageView) findViewById(R.id.navigation_imgCalendar);
        ImageView imgWorkout	= (ImageView) findViewById(R.id.navigation_imgWorkout);
        ImageView imgExercise	= (ImageView) findViewById(R.id.navigation_imgExercise);
        
        imgProfile.setOnClickListener(this);
        imgCalendar.setOnClickListener(this);
        imgWorkout.setOnClickListener(this);
        imgExercise.setEnabled(false);
        imgExercise.setImageResource(R.drawable.exercises_icon_trans_dark);
        // TODO Set this through R.color
        imgExercise.setBackgroundColor(MainActivity._BG_BLUE);
        
        showMuscleGroups();
        
	} // end onCreate()
	
	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
			case R.id.navigation_imgProfile:
				MainActivity.saveData();
				intent = new Intent(this, ProfileActivity.class);
				startActivity(intent);
				finish();
				break;
			case R.id.navigation_imgCalendar:
				MainActivity.saveData();
				finish();
				break;
			case R.id.navigation_imgWorkout:
				MainActivity.saveData();
				intent = new Intent(this, WorkoutActivity.class);
				startActivity(intent);
				finish();
				break;
			case R.id.exercise_imgBack:
				if (_currentScreen == Screens.MUSCLEGROUP) {
					MainActivity.saveData();
					finish();
				} else if (_currentScreen == Screens.EXERCISE) {
					showMuscleGroups();
				}
				break;
			case R.id.exercise_imgSettings:
				MainActivity.saveData();
				intent = new Intent(this, SettingsActivity.class);
				startActivity(intent);
				break;
		}
	} // end override onClick()
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	        if (_currentScreen == Screens.MUSCLEGROUP) {
	        	finish();
	        	return true;
	        } else if (_currentScreen == Screens.EXERCISE) {
	        	showMuscleGroups();
	        }
	    }
	    return super.onKeyDown(keyCode, event);
	} // end override onKeyDown
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	protected final void clearScreen() {
		_llyMain.removeAllViews();
	} // end clearScreen()
	
	protected final void showMuscleGroups() {
		clearScreen();
		_currentScreen = Screens.MUSCLEGROUP;
		
		LinearLayout lly = new LinearLayout(this);
		lly.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		lly.setOrientation(LinearLayout.VERTICAL);
		
		Button btn;
		
		String[] muscleGroups = getResources().getStringArray(R.array.musclegroups);
		
		for (String s : muscleGroups) {
			btn = new Button(this);
			
			btn.setText(s);
			btn.setOnClickListener(new HandleMuscleGroup(s));
			btn.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			
			lly.addView(btn);
		} // end foreach 
		
		
		_llyMain.addView(lly);
	} // end showMuscleGroups()
	
	protected final void showExercises(String muscleGroup) {
		clearScreen();
		_currentScreen = Screens.EXERCISE;
		
		LinearLayout lly = new LinearLayout(this);
		lly.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		lly.setOrientation(LinearLayout.VERTICAL);
		
		Button btn;
		
		String[] exercises = {"debugexercisename"};
		
		if (muscleGroup.equals("Abs")) {
			exercises = getResources().getStringArray(R.array.exercises_abs);
		} else if (muscleGroup.equals("Back")) {
			exercises = getResources().getStringArray(R.array.exercises_back);
		} else if (muscleGroup.equals("Biceps")) {
			exercises = getResources().getStringArray(R.array.exercises_biceps);
		} else if (muscleGroup.equals("Chest")) {
			exercises = getResources().getStringArray(R.array.exercises_chest);
		} else if (muscleGroup.equals("Forearm")) {
			exercises = getResources().getStringArray(R.array.exercises_forearm);
		} else if (muscleGroup.equals("Glutes")) {
			exercises = getResources().getStringArray(R.array.exercises_glutes);
		} else if (muscleGroup.equals("Hamstring")) {
			exercises = getResources().getStringArray(R.array.exercises_hamstring);
		} else if (muscleGroup.equals("Quads")) {
			exercises = getResources().getStringArray(R.array.exercises_quads);
		} else if (muscleGroup.equals("Shoulder")) {
			exercises = getResources().getStringArray(R.array.exercises_shoulder);
		} else if (muscleGroup.equals("Triceps")) {
			exercises = getResources().getStringArray(R.array.exercises_triceps);
		}
		
		for (String s : exercises) {
			btn = new Button(this);
			
			btn.setText(s);
			//btn.setOnClickListener(new handleMuscleGroup(s));
			btn.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			
			lly.addView(btn);
		}
		
		// Add custom exercises
		
		RelativeLayout rly;
		RelativeLayout.LayoutParams rlp;
		ImageView imgDelete, imgEdit;
		TextView tv;
		int eNameID = 101, eEditID = 102, eDeleteID = 103;
		Exercise e;
		
		for (int i = 0; i < MainActivity._exercises.size(); i++) {
			e = MainActivity._exercises.get(i);
			if (!e.getMuscleGroup().equals(muscleGroup)) {
				continue;
			}
			
			// Layout that will hold llyDetails, btnEdit, and btnDelete for the Exercise
			rly = new RelativeLayout(this);
			rly.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
			
			// Delete Button
			imgDelete = new ImageView(this);
			imgDelete.setImageResource(R.drawable.icon_delete);
			imgDelete.setId(eDeleteID);
			imgDelete.setOnClickListener(new DeleteExerciseHandler(i, muscleGroup));
			rlp = new RelativeLayout.LayoutParams(new LayoutParams(100,LayoutParams.WRAP_CONTENT));
			rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
			
			rly.addView(imgDelete, rlp);
			
			// Edit Button
			imgEdit = new ImageView(this);
			imgEdit.setImageResource(R.drawable.icon_edit);
			imgEdit.setId(eEditID);
			imgEdit.setOnClickListener(new PopupHandler(true, i, muscleGroup));
			rlp = new RelativeLayout.LayoutParams(new LayoutParams(100,LayoutParams.WRAP_CONTENT));
			rlp.addRule(RelativeLayout.LEFT_OF, imgDelete.getId());
			
			rly.addView(imgEdit, rlp);
			
			// Name of new exercise
			tv = new TextView(this);
			tv.setText(e.getName());
			tv.setTextSize(20f);
			tv.setGravity(Gravity.CENTER);
			tv.setId(eNameID);
			rlp = new RelativeLayout.LayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
			rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
			rlp.addRule(RelativeLayout.LEFT_OF, imgEdit.getId());
			
			rly.addView(tv, rlp);
			
			lly.addView(rly);
		}
		
		// Add new exercise button
		btn = new Button(this);
		btn.setText("Add Exercise");
		btn.setOnClickListener(new PopupHandler(false, -1, muscleGroup));
		btn.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		btn.setBackgroundResource(R.drawable.button_one);
		
		lly.addView(btn);
		
		_llyMain.addView(lly);
		
	} // end showExercises()
	
	protected final void addExercise(boolean edit, int exslot, String muscleGroup) {
		PopupExercise popEx = new PopupExercise(edit, exslot, muscleGroup);
		popEx.showDialog();
	}
	
	protected final void showToast(String text) {
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	} // end showToast()
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	private final class HandleMuscleGroup implements OnClickListener {
		
		private String _muscleGroup;
		
		public HandleMuscleGroup(String muscleGroup) {
			_muscleGroup = muscleGroup;
		}
		
		@Override
		public void onClick(View v) {
			showExercises(_muscleGroup);
		}
	} // end inline class handleMuscleGroup
	
	private final class PopupHandler implements OnClickListener {
		private boolean _edit;
		private int _slot;
		private String _muscleGroup;
		
		public PopupHandler(boolean edit, int slot, String muscleGroup) {
			_edit 			= edit;
			_slot 			= slot;
			_muscleGroup 	= muscleGroup;
		}
		
		@Override
		public void onClick(View v) {
			addExercise(_edit, _slot, _muscleGroup);
		}
		
	} // end inner class PopupHandler
	
	private final class PopupExercise implements OnClickListener {
		
		private final Dialog 	_d;
		private Exercise		_e;
		private boolean 		_edit;
		private int 			_exslot;
		private String 			_muscleGroup;
		private EditText		_edtName;
		
		public PopupExercise(boolean edit, int exslot, String muscleGroup) {
			_edit = edit;
			_exslot = exslot;
			_muscleGroup = muscleGroup;
			// Pop up add exercise
			_d = new Dialog(ExerciseActivity.this, R.style.dialogstyle);
			_d.setContentView(R.layout.exercise_custom);
			
			_edtName		 	= (EditText) _d.findViewById(R.id.exercisecustom_edtName);
			Button btnAdd		= (Button)  _d.findViewById(R.id.exercisecustom_btnAdd);
			Button btnCancel	= (Button)  _d.findViewById(R.id.exercisecustom_btnCancel);
			
			btnAdd.setOnClickListener(this);
			btnCancel.setOnClickListener(this);
			
			if (_edit) {
				_d.setTitle("Edit Exercise");
				_e = MainActivity._exercises.get(_exslot);
				_edtName.setText(_e.getName());
			} else {
				_d.setTitle("Add Exercise");
				_e = new Exercise();
			}
			
		} // end constructor()
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.exercisecustom_btnAdd:
					
					if (_edtName.getText().toString().length() > 0) {
						_e.setName(_edtName.getText().toString());
						_e.setMuscleGroup(_muscleGroup);
						// TODO Do this through a mutator
						MainActivity._exercises.add(_e);
						MainActivity.saveData();
						MainActivity.loadData();
						_d.dismiss();
						showExercises(_muscleGroup);
					}
					
					break;
				case R.id.exercisecustom_btnCancel:
					_d.dismiss();
					break;
			}
		}
		
		public void showDialog() {
			_d.show();
		}
		
	} // end inline class AddExercisePopup
	
	private final class DeleteExerciseHandler implements OnClickListener {
		
		private int _exslot;
		private String _muscleGroup;
		
		public DeleteExerciseHandler(int exslot, String muscleGroup) {
			_exslot = exslot;
			_muscleGroup = muscleGroup;
		}
		
		@Override
		public void onClick(View v) {
			MainActivity._exercises.remove(_exslot);
			MainActivity.saveData();
			MainActivity.loadData();
			showExercises(_muscleGroup);
		}
	}
	
} // end class ExerciseActivity